#include "CustomEventReceiver.h"
#include "EventTypes.h"
#include "WorldHandler.h"
#include "ScreenHandler.h"


CustomEventReceiver::CustomEventReceiver()
{
	for(unsigned int i = 0; i < KEY_KEY_CODES_COUNT; i++)
	{
		keyIsDown[i] = false;
		keyIsHandled[i] = false;
	}
	
	mouseState.RightButtonDown = false;
	mouseState.LeftButtonDown = false;
	mouseState.RightButtonEventHandled = false;
	mouseState.LeftButtonEventHandled = false;
}

MouseState* CustomEventReceiver::getMouseState()
{
	return &mouseState;
}

bool CustomEventReceiver::OnEvent(const SEvent &event)
{
	if(event.EventType == EET_MOUSE_INPUT_EVENT)
	{
		switch(event.MouseInput.Event)
		{
			case EMIE_RMOUSE_PRESSED_DOWN:
				mouseState.RightButtonDown = true;
				break;
			case EMIE_RMOUSE_LEFT_UP:
				mouseState.RightButtonDown = false;
				mouseState.RightButtonEventHandled = false;
				break;
			case EMIE_LMOUSE_PRESSED_DOWN:
				mouseState.LeftButtonDown = true;
				break;
			case EMIE_LMOUSE_LEFT_UP:
				mouseState.LeftButtonDown = false;
				mouseState.LeftButtonEventHandled = false;
				break;
			case EMIE_MOUSE_MOVED:
				mouseState.Position.X = event.MouseInput.X;
				mouseState.Position.Y = event.MouseInput.Y;
				break;
			case EMIE_MOUSE_WHEEL:
				mouseState.mouseDelta = event.MouseInput.Wheel;
				break;
			default:
				break;
		}
	}
	
	if(event.EventType == EET_KEY_INPUT_EVENT)
	{
		if(event.KeyInput.PressedDown)
		{
			keyIsDown[event.KeyInput.Key] = true;
		}
		else
		{
			keyIsDown[event.KeyInput.Key] = false;
			keyIsHandled[event.KeyInput.Key] = false;
		}

		return false;
	}
	
	if (event.EventType == EET_GUI_EVENT)
	{
	#if 0
		s32 id = event.GUIEvent.Caller->getID();
		gui::IGUIEnvironment* env = globalScreenHandler.getDevice()->getGUIEnvironment();

		switch(event.GUIEvent.EventType)
		{
			case gui::EGET_BUTTON_CLICKED:
				switch(id)
				{
					case BUTTONID_TRADEEXIT:
						globalScreenHandler.setShowTradeMenu(false);
						return true;
					case BUTTONID_DUMMYITEM:
						return true;
				}
				break;
		}
		#endif
	}

	return false;
}

bool CustomEventReceiver::isKeyDown(EKEY_CODE keyCode) const
{
	if(keyCode > KEY_KEY_CODES_COUNT)
	{
		return false;
	}

	return keyIsDown[keyCode];
}

bool CustomEventReceiver::isKeyHandled(EKEY_CODE keyCode) const
{
	if(keyCode > KEY_KEY_CODES_COUNT)
	{
		return false;
	}

	return keyIsHandled[keyCode];
}


void CustomEventReceiver::setKeyDown(EKEY_CODE keyCode, bool passedState)
{
	keyIsDown[keyCode] = passedState;
}

void CustomEventReceiver::setKeyHandled(EKEY_CODE keyCode, bool passedState)
{
	keyIsHandled[keyCode] = passedState;
}

void CustomEventReceiver::setLeftButtonEventHandled(bool passedHandled)
{
	mouseState.LeftButtonEventHandled = passedHandled;
}

void CustomEventReceiver::setRightButtonEventHandled(bool passedHandled)
{
	mouseState.RightButtonEventHandled = passedHandled;
}
